package com.adcc.loadsheet.billing.controller;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.billing.exception.ParamValidException;
import com.adcc.loadsheet.billing.exception.PermissionVerifyException;
import com.adcc.loadsheet.billing.service.LogService;
import com.adcc.loadsheet.billing.util.LogUtil;
import com.adcc.loadsheet.billing.vo.*;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 日志查询Controller层
 * Created by zhaoml on 2020/9/9.
 */
@Controller
@RequestMapping(value = "log")
@Api(value = "/log", description = "日志查询相关操作controller")
public class LogController extends BasisController{

    @Autowired
    LogService logService;

    /**
     * 进入日志查询页面
     * @param model
     * @return
     */
    @PreAuthorize("hasPermission('','LogQuery')")
    @GetMapping(value = "tologquery")
    @ApiOperation(value = "/tologquery", notes = "跳转到日志查询页面", httpMethod = "GET")
    public String toLogQuery(Model model) {
        return "log/log-query";
    }

    //查询日志
    @PostMapping(value = "findLog", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiOperation(value = "/findLog", notes = "查询日志", httpMethod = "POST")
    @ResponseBody
    public ResultVO findLog(@RequestBody Map<String, Object> map) {
        ResultVO vo = new ResultVO();
        try {
            Map<String, Object> paramMap = inputContractUserParams(map);

            //日志类型
            Integer logType = map.get("logType") == null || "".equals(map.get("logType")) ? null : Integer.parseInt(map.get("logType").toString());
            if (logType != null && LogUtil.ERROR_LOG_TYPE == logType) {  //异常日志
                PageInfo<SystemErrorLogEntityVO> systemErrorLogPageInfo = logService.selectSystemErrorLogForPage(paramMap);
                vo.setStatus(ResultStatus.SUCCESS);
                vo.getData().put("voList", systemErrorLogPageInfo);
            }else if (logType != null && LogUtil.JOB_LOG_TYPE == logType) { //JOB日志
                PageInfo<SystemJobLogEntityVO> systemJobLogPageInfo = logService.selectSystemJobLogForPage(paramMap);
                vo.setStatus(ResultStatus.SUCCESS);
                vo.getData().put("voList", systemJobLogPageInfo);
            }else {     //系统操作日志
                PageInfo<SystemOperationLogEntityVO> systemOperationLogPageInfo = logService.selectSystemOperationLogForPage(paramMap);
                vo.setStatus(ResultStatus.SUCCESS);
                vo.getData().put("voList", systemOperationLogPageInfo);
            }
        } catch (Exception ex) {
            Log.error(LogController.class, "查询日志出错", ex);
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_CALL_EX_FAILED, "Query findLog", ex.fillInStackTrace()));
            LogUtil.errorLog(LogController.class,ex,"findLog()");
        }
        return vo;
    }

    private Map<String, Object> inputContractUserParams(Map<String, Object> map) throws
            PermissionVerifyException, ParamValidException {
        Map<String, Object> paramMap = Maps.newHashMap();
        try {
            //开始时间
            String startTime = map.get("startDate") == null || "".equals(map.get("startDate")) ? "" : map.get("startDate").toString() + " 00:00:00";
            //结束时间
            String endTime = map.get("endDate") == null || "".equals(map.get("endDate")) ? "" : map.get("endDate").toString() + " 23:59:59";
            //操作人
            String operator = map.get("operator") == null || "".equals(map.get("operator")) ? "" : "%" + map.get("operator").toString() + "%";
            //内容详情
            String content = map.get("content") == null || "".equals(map.get("content")) ? "" : "%" + map.get("content").toString() + "%";
            //日志类型
            Integer operationType = map.get("logType") == null || "".equals(map.get("logType")) || "null".equals(map.get("logType")) ? null : Integer.parseInt(map.get("logType").toString());
            //第几页
            Integer pageNum = map.get("pageNum") == null || "".equals(map.get("pageNum")) ? 1 : Integer.parseInt(map.get("pageNum").toString());
            //每页页数
            Integer pageSize = map.get("pageSize") == null || "".equals(map.get("pageSize")) ? PAGE_SIZE_DEFAULT : Integer.parseInt(map.get("pageSize").toString());

            paramMap.put("startTime", startTime);
            paramMap.put("endTime", endTime);
            paramMap.put("operator", operator);
            paramMap.put("content", content);
            paramMap.put("operationType", operationType);
            paramMap.put("pageNum", pageNum);
            paramMap.put("pageSize", pageSize);
        } catch (Exception e) {
            throw new ParamValidException("输入参数错误");
        }
        return paramMap;
    }
}